﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Net;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Threading;
using Coding4Fun.Toolkit.Controls;
using ImageTools.IO.Gif;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Telerik.Windows.Controls;
using TestMatrix.BLController;
using TestMatrix.Model;
using ImageTools.Controls;

namespace TestMatrix.Phone.JumpList
{
    public partial class ImageButton : PhoneApplicationPage, INotifyPropertyChanged
    {
        int result = 0;
        int i = 0;
        private SolidColorBrush backgroundBrush;
        public SolidColorBrush BackgroundBrush
        {
            get
            {
                return this.backgroundBrush;
            }
            set
            {
                if (this.backgroundBrush != value)
                {
                    this.backgroundBrush = value;
                    if (this.PropertyChanged != null)
                    {
                        this.PropertyChanged(this, new PropertyChangedEventArgs("BackgroundBrush"));
                    }
                }
            }
        }
        public ImageButton()
        {
           
            InitializeComponent();

            

            Storyboard storyboardIn = this.Resources["MyStoryBoardIn"] as Storyboard;
            storyboardIn.SpeedRatio = 1;
            storyboardIn.Begin();


            DispatcherTimer timer = new DispatcherTimer();
            timer.Tick += new EventHandler(timer_Tick);
            timer.Interval = TimeSpan.FromSeconds(60);
            timer.Start();

            //RotateInit();
            ImageBrush b = new ImageBrush();
            b.ImageSource = new BitmapImage(new Uri(@"Images\bg2.jpg", UriKind.Relative));
            //LayoutRoot.Background = b;

            ImageTools.IO.Decoders.AddDecoder<GifDecoder>();
            ImageTools.ExtendedImage image = new ImageTools.ExtendedImage();
            image.UriSource = new Uri("http://i731.photobucket.com/albums/ww312/EGAGUN/TF2/Bumblebee5.gif", UriKind.Absolute);
            imgGif.Source = image;

        }
        private void GetBugDetails()
        {
            string sql = "select count(*) as Count from tblBugList";
            List<tblBugList> listDatas = ChartController.Query(sql);
            int bugCnt = listDatas[0].Count;

            if (result >= bugCnt)
            {
                result = bugCnt;
            }

            var bugCounts = new { Name = "Bug: " + bugCnt.ToString() };
            this.CameraButton.Content = bugCounts;
        }
        private void GetTCDetails()
        {
            string sql = "select * from tblTestAutomation";
            List<tblTestAutomation> listDatas = ChartController.QueryTblTestAutomation(sql);
            int bugCnt = listDatas.Count;

            if (result >= bugCnt)
            {
                result = bugCnt;
            }

            var bugCounts = new { Name = "Test Case: " + bugCnt.ToString() };
            this.MusicButton.Content = bugCounts;
        }

        private void GetMTMPassRate()
        {
            //string sql = "select avg(cast(PassPercent as float)) as AvgPercent from tblGlobalTestCasePassRate";
            //List<tblGlobalTestCasePassRate> listDatas = ChartController.QuerytblGolbalTestCasePassRate(sql);
            //int bugCnt = listDatas.Count;
           
            //if (result >= bugCnt)
            //{
            //    result = bugCnt;
            //}
            var bugCounts = new { Name = "MTM PassRate: 83.24%" };
            this.PlaceButton.Content = bugCounts;
        }

        private void GetReleaseDetails()
        {
            //select sum(ReleaseCnt) from tblReleaseInfo
            var bugCounts = new { Name = "ReleaseCnt: 666" };
            this.PeopleButton.Content = bugCounts;
        }

        private void GetGlobalTCAutomation()
        {
            //select sum(Automated) from tblGlobalTestCaseAutomation
            var bugCounts = new { Name = "TCAuto: 53397" };
            this.HeartButton.Content = bugCounts;
        }

        private void GetGlobalDRE()
        {
            //select avg(cast(DRE as float)) from tblGlobalDRE
            var bugCounts = new { Name = "DRE: 86" };
            this.ChatButton.Content = bugCounts;
        }


        private void timer_Tick(object sender, EventArgs e)
        {
            Random rdm = new Random();
            int r = rdm.Next(50, 100);
            result += r;
           
            GetBugDetails();
            GetTCDetails();
            GetMTMPassRate();
            GetReleaseDetails();
            GetGlobalTCAutomation();
            GetGlobalDRE();
        }
        private void RotateInit()
        {
            RadScaleMoveAndRotateAnimation scaleMoveRotateAnimation = this.Resources["scaleMoveRotateAnimation"] as RadScaleMoveAndRotateAnimation;

            scaleMoveRotateAnimation.MoveAnimation.StartPoint = new Point(0, 0);
            scaleMoveRotateAnimation.MoveAnimation.EndPoint = new Point(0, 0);

            scaleMoveRotateAnimation.RotateAnimation.Axes = PerspectiveAnimationAxis.Z;

            scaleMoveRotateAnimation.RotateAnimation.StartAngleZ = 0;
            scaleMoveRotateAnimation.RotateAnimation.StartAngleZ = 60;

            scaleMoveRotateAnimation.RotateAnimation.Direction = PerspectiveAnimationDirection.CounterClockwise;
            scaleMoveRotateAnimation.RotateAnimation.SpeedRatio = 0.1;

            //scaleMoveRotateAnimation.RotateAnimation.RepeatBehavior = System.Windows.Media.Animation.RepeatBehavior.Forever;
            if (scaleMoveRotateAnimation != null)
            {
                //RadAnimationManager.Play(this.CenterButton, scaleMoveRotateAnimation);
                //RadAnimationManager.Play(this.CameraButton, scaleMoveRotateAnimation);
                //RadAnimationManager.Play(this.MusicButton, scaleMoveRotateAnimation);
                //RadAnimationManager.Play(this.PeopleButton, scaleMoveRotateAnimation);
                //RadAnimationManager.Play(this.PlaceButton, scaleMoveRotateAnimation);
                //RadAnimationManager.Play(this.HeartButton, scaleMoveRotateAnimation);
                //RadAnimationManager.Play(this.ChatButton, scaleMoveRotateAnimation);
            }
        }

        private void BugButton_Click(object sender, RoutedEventArgs e)
        {
            #region popup page
            //Storyboard storyboardOut = this.Resources["MyStoryBoardOut"] as Storyboard;
            //storyboardOut.SpeedRatio = 3;
            //storyboardOut.Begin();

            //storyboardOut.Completed += (sd, ea) =>
            //{
            //    if (i == 0)
            //    {
            //        MessagePrompt messagePrompt = new MessagePrompt();
            //        messagePrompt.Title = " Bug Details";
            //        BugUserControl uc = new BugUserControl();
            //        messagePrompt.Body = uc;

            //        messagePrompt.IsCancelVisible = false;
            //        messagePrompt.Margin = new Thickness(10, 100, 10, 10);
            //        messagePrompt.Background = new SolidColorBrush(Color.FromArgb(50, 230, 230, 250));
            //        messagePrompt.Completed += (aa, bb) =>
            //        {
            //            Storyboard storyboardIn = this.Resources["MyStoryBoardIn"] as Storyboard;
            //            storyboardIn.SpeedRatio = 1;
            //            storyboardIn.Begin();
            //            i = 0;
            //        };

            //        messagePrompt.Show();
            //        i++;
            //    }

            //};
            #endregion
            NavigationService.Navigate(new Uri("/MatrixSummaryPage.xaml", UriKind.Relative));
        }
        private void TestCaseButton_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/TestCaseReport.xaml", UriKind.Relative));
        }
        private void ReleaseButton_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/ReleaseDetailInfoPage.xaml", UriKind.Relative));
        }
        private void GlobalTCPassRateButton_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/GlobalTestCasePassRatePage.xaml", UriKind.Relative));
        }
        private void GlobalTCAutomationButton_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/GlobalTestCaseAutomation.xaml", UriKind.Relative));
        }
        private void GlobalDREButton_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/GlobalDREPage.xaml", UriKind.Relative));
        }


        private void testbtn_Click(object sender, RoutedEventArgs e)
        {
            MessagePrompt messagePrompt = new MessagePrompt();
            messagePrompt.Hide();
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void DoubleAnimation_Completed(object sender, EventArgs e)
        {
            //RotateInit();
            
        }

        
        
    }
}